package common

import "github.com/gin-gonic/gin"

const CODE_OK = 0
const CODE_FAIL = -1
const CODE_ERROR = 500

func ResSuccess(c *gin.Context, data interface{}) {
	c.JSON(200, NewResponse(CODE_OK, "ok", data, 0))
}
func ResSuccessWithTotal(c *gin.Context, data interface{}, total int64) {
	c.JSON(200, NewResponse(CODE_OK, "ok", data, total))
}

func ResFail(c *gin.Context, msg string) {
	c.JSON(200, NewResponse(CODE_FAIL, msg, "", 0))
}
func ResFailWitgCode(c *gin.Context, code int, msg string) {
	if code == CODE_OK {
		code = CODE_FAIL
	}
	c.JSON(200, NewResponse(code, msg, "", 0))
}

func ResError(c *gin.Context, msg string) {
	c.JSON(200, NewResponse(CODE_ERROR, msg, "", 0))
}

func NewResponse(code int, msg string, data interface{}, total int64) gin.H {
	pack := gin.H{
		"code":  code,
		"msg":   msg,
		"data":  data,
		"total": total,
	}
	return pack
}
